package fr.zadoka.zadokadna;

import java.util.GregorianCalendar;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Pair;

public class ZadokaFavorites {
	
	//favorite file manager
	private ZadokaFileManager fileManager;
	
	public class ZadokaFilePair extends Pair<GregorianCalendar, Bitmap>
	{

		public ZadokaFilePair(GregorianCalendar first, Bitmap second) {
			super(first, second);
		}
		
	}
	
	public ZadokaFavorites(Context context) {
		fileManager = new ZadokaFileManager(context);
	}
		
	public Bitmap loadImage(final GregorianCalendar imageDate)
	{
		return fileManager.loadImage(imageDate);
	}
	
	public void deleteImage(GregorianCalendar imageDate) {
		new DeleteFileTask(fileManager).execute(imageDate);
	}

	public void saveImage(GregorianCalendar imageDate, Bitmap image) {
		new SaveFileTask(fileManager).execute(new ZadokaFilePair(imageDate, image));
		
	}

	public class SaveFileTask extends AsyncTask<ZadokaFilePair, Void, Void>
	{

		private ZadokaFileManager mFileManager;

		public SaveFileTask(ZadokaFileManager fileManager)
		{
			mFileManager = fileManager;
		}
		
		@Override
		protected Void doInBackground(ZadokaFilePair... params) {
			
			GregorianCalendar imageDate = params[0].first;
			Bitmap image = params[0].second;
			 
			mFileManager.saveImage(imageDate, image);

			return null;
		}
	
	}

	public class DeleteFileTask extends AsyncTask<GregorianCalendar, Void, Void>
	{

		private ZadokaFileManager mFileManager;

		public DeleteFileTask(ZadokaFileManager fileManager)
		{
			mFileManager = fileManager;
		}
		
		@Override
		protected Void doInBackground(GregorianCalendar... params) {
			
			GregorianCalendar imageDate = params[0];
			 
			mFileManager.deleteImage(imageDate);

			return null;
		}
		
	}
	
}
